<?php
/**
 * Created by PhpStorm.
 * User: hengsu
 * Date: 2016/5/23
 * Time: 17:56
 */

namespace Home\Controller;
use Think\Controller;

class UserController extends Controller{
    public function userInfo(){
        $userID=isset($_POST['userId'])?trim($_POST['userId']):null;       //页码
        if(empty($userID)){
            $requset=array(
                'returnCode' => 998,
                'returnMsg' => '参数错误'
            );
            echo json_encode($requset); exit;
        }
        $model=M('User');
        $userinfo=$model->where('id='.$userID)->find();
        if(empty($userinfo)){
            $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
        }else{
            $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$userinfo);
        }
        echo json_encode($requset); exit;
    }
    public function userEdit(){
        $userID=isset($_POST['userId'])?trim($_POST['userId']):null;
        $nickname=isset($_POST['nickname'])?trim($_POST['nickname']):null;
        $avatar=isset($_POST['avatar'])?trim($_POST['avatar']):null;
        $sex=isset($_POST['sex'])?trim($_POST['sex']):null;
        $phone=isset($_POST['phone'])?trim($_POST['phone']):null;
        $name=isset($_POST['name'])?trim($_POST['name']):null;
        $address=isset($_POST['address'])?trim($_POST['address']):null;
        $note=isset($_POST['note'])?trim($_POST['note']):null;
        $data=array();
        $data['id']=$userID;
        if(!empty($nickname)){
            $data['nickname']=$nickname;
        }
        if(!empty($avatar)){
            $data['avatar']=$avatar;
        }
        if(!empty($sex)){
            $data['sex']=$sex;
        }
        if(!empty($phone)){
            $data['phone']=$phone;
        }
        if(!empty($name)){
            $data['name']=$name;
        }
        if(!empty($address)){
            $data['address']=$address;
        }
        if(!empty($note)){
            $data['note']=$note;
        }
        $model=M('User');
        if($model->save($data)){
            $requset=array('returnCode'=>0,'returnMsg'=>'成功');
        }else{
            $requset=array('returnCode'=>999,'returnMsg'=>'失败');
        }
        echo json_encode($requset); exit;
    }
    public function freeNum(){
        $userID=isset($_POST['userId'])?trim($_POST['userId']):null;
        $catId=isset($_POST['catId'])?trim($_POST['catId']):null;
        $quesOrder=M('Questions_order');
        $orderli=$quesOrder->where(array('user_id'=>$userID,'cat_id'=>$catId,'status'=>1))->find();
        if(empty($orderli)){
                $free=M('Free');
                $count=$free->where(array('user_id'=>$userID,'cat_id'=>$catId))->count();
                if($count < 1){

                    $totalli=D('Category')->field('total')->where('id='.$catId)->find();
                    $free->add(array('user_id'=>$userID,'cat_id'=>$catId,'total'=>$totalli['total']));
                }
                $freeli=$free->field('total')->where(array('user_id'=>$userID,'cat_id'=>$catId))->find();
                if(0<(int)$freeli['total']){
                    $request=array('returnCode'=>0,'returnMsg'=>'success','returnData'=>array('total'=>$freeli['total']));
                }else{
                    $request=array('returnCode'=>999,'returnMsg'=>'error');
                }
        }else{
            $request=array('returnCode'=>0,'returnMsg'=>'success');
        }
        echo json_encode($request);
    }
}